package com.itheima02;

import java.lang.reflect.Field;

public class Test04 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Student stu = new Student();

        setProperty(stu, "name", "张三");

        System.out.println(stu);
    }

    public static void setProperty(Object obj, String propertyName, Object value) throws NoSuchFieldException, IllegalAccessException {

        Class clazz = obj.getClass();

        Field field = clazz.getDeclaredField(propertyName);

        field.setAccessible(true);

        field.set(obj, value);

    }
}
